package com.example.applets.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.applets.entity.Menu;
import com.example.applets.entity.Menupriv;
import com.example.applets.service.impl.MenuServiceImpl;
import com.example.applets.service.impl.MenuprivServiceImpl;
import com.example.applets.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 云烟成雨丶
 * @since 2020-10-09
 */
@Controller
@RequestMapping("/applets/menu")
public class MenuController {
    @Autowired
    private MenuServiceImpl menuService;

    @Autowired
    private MenuprivServiceImpl menuprivService;

    @RequestMapping("/page")
    @ResponseBody
    public AjaxResult selectPage(@RequestParam("mtext") String mtext, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        //分页
        Page<Menu> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        //条件构造器
        if (!mtext.equals(""))
            wrapper.like("mtext", mtext);
        //获得集合
        List<Menu> list = menuService.page(page, wrapper).getRecords();
        //获得页数
        int count = (menuService.count(wrapper) - 1) / pageSize + 1;
        //创建map集合
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("total", count);
        if (list.size() == 0)
            return AjaxResult.error("暂无数据");
        return AjaxResult.success(map);
    }

    @RequestMapping("/add")
    @ResponseBody
    //新增菜单
    public AjaxResult addMenu(Menu menu) {
        if (!menuService.save(menu))
            return AjaxResult.error("新增失败!");
        return AjaxResult.success("新增成功!");
    }

    @RequestMapping("/select")
    //查找单个
    public String selectMenu(@RequestParam("id") Long id, Model model) {
        Menu menu = menuService.getById(id);
        model.addAttribute("m", menu);
        return "KMenu/UpdateMenu";
    }

    @RequestMapping("/update")
    @ResponseBody
    //修改用户
    public AjaxResult updateMenu(Menu menu) {
        if (!menuService.updateById(menu))
            return AjaxResult.error("修改失败!");
        return AjaxResult.success("修改成功!");
    }

    @RequestMapping("/delete")
    @ResponseBody
    //删除用户
    public AjaxResult deleteMenu(@RequestParam("id") Long id) {
        // Menu menu = menuService.getById(id);
        QueryWrapper<Menupriv> wrapper = new QueryWrapper<>();
        wrapper.eq("menuid", id);
        List<Menupriv> list = menuprivService.list(wrapper);
        if (list.size() > 0)
            return AjaxResult.error("该菜单被占用,无法删除!");
        if (!menuService.removeById(id))
            return AjaxResult.error("删除失败!");
        return AjaxResult.success("删除成功!");
    }

    @RequestMapping("/getAll")
    @ResponseBody
    //修改用户
    public AjaxResult getMenu() {
        List<Menu> list = menuService.list();
        if (list.size() == 0)
            return AjaxResult.error("暂无数据!");
        return AjaxResult.success(list);
    }

}

